package com.yaodian100.ec.generatepage.web;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.generate.entity.GenerateAdvertise;
import com.yaodian100.ec.generate.entity.GenerateContent;
import com.yaodian100.ec.generate.entity.GeneratePosition;
import com.yaodian100.ec.generate.service.GenerateService;

/**
 *
 * @author aaron.chueh
 */
public class Top4TagAction extends ActionSupport implements Preparable {
	
	private static final long serialVersionUID = 9123279318759450966L;
	
	private final Logger logger = LoggerFactory.getLogger(Top4TagAction.class);
	
	private GenerateService generateService;
	private GenerateAdvertise advertise;
	private GeneratePosition position;
	private GenerateContent content;
	private StringBuilder contents;
	
	private String advType;
	private String advStatus;// 00:草稿,10:发送,90:取消
	private String advId;
	
	private String advName;
	private String startDate;// 开始日期
	private String endDate;// 结束日期
	private String startTime;// 开始时间
	private String endTime;// 结束时间
	private Date createDate;
	private String createUser;
	private Date modifyDate;
	private String modifyUser;
	
	private String title1Name;
	private String isHighlight1;
	private String title1Url;
	private String title1adWord;
	
	private String title2Name;
	private String isHighlight2;
	private String title2Url;
	private String title2adWord;
	
	private String title3Name;
	private String isHighlight3;
	private String title3Url;
	private String title3adWord;
	
	private String title4Name;
	private String isHighlight4;
	private String title4Url;
	private String title4adWord;
	
	private final DateFormat PATTERN_DATE_FORMAT = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP);

	@Override
	public void prepare() throws Exception {
		clearErrorsAndMessages();
	}
	
	/**
	 * 四头标新增
	 * @return
	 * @throws Exception
	 */
	public String addTop4Tag() throws Exception {
		advType="TOP4_TAG";
		
		return "editTop4Tag";
	}
	
	/**
	 * 四头标修改
	 * @return
	 * @throws Exception
	 */
	public String editTop4Tag() throws Exception {
		initVariables();
		advertise = generateService.get(Long.parseLong(advId));
		position = generateService.getGeneratePositionByAdvId(Long.parseLong(advId)).get(0);
		content = generateService.getGenerateContentByAdvId(Long.parseLong(advId)).get(0);
		
		advStatus = advertise.getAdvStatus();
		advType = advertise.getAdvType();
		advName = advertise.getAdvName();
		startDate = FastDateFormat.getInstance(EcConstant.PATTERN_DATE).format(advertise.getStartDate());
		endDate = FastDateFormat.getInstance(EcConstant.PATTERN_DATE).format(advertise.getEndDate());
		startTime = FastDateFormat.getInstance("HH:mm").format(advertise.getStartDate());
		endTime = FastDateFormat.getInstance("HH:mm").format(advertise.getEndDate());
		createDate = advertise.getCreateDate();
		createUser = advertise.getCreateUser();
		modifyDate = advertise.getModifyDate();
		modifyUser = advertise.getModifyUser();
		
		readPropertise(content);
		
		return "editTop4Tag";
	}
	
	public String cloneTop4Tag() throws Exception {
		initVariables();
		
		advertise = generateService.get(Long.parseLong(advId));
		position = generateService.getGeneratePositionByAdvId(Long.parseLong(advId)).get(0);
		content = generateService.getGenerateContentByAdvId(Long.parseLong(advId)).get(0);
		
		GenerateAdvertise tmp_advertise = new GenerateAdvertise();
		BeanUtils.copyProperties(advertise, tmp_advertise, new String[] {"id", "createDate", "createUser", "modifyUser", "modifyDate"});
		advertise = tmp_advertise;
		
		GeneratePosition tmp_position = new GeneratePosition();
		BeanUtils.copyProperties(position, tmp_position, new String[] {"id", "advId", "createDate", "createUser", "modifyUser", "modifyDate"});
		position = tmp_position;
		
		GenerateContent tmp_content = new GenerateContent();
		BeanUtils.copyProperties(content, tmp_content, new String[] {"id", "advId", "createDate", "createUser", "modifyUser", "modifyDate"});
		content = tmp_content;
		
		advStatus = advertise.getAdvStatus();
		advType = advertise.getAdvType();
		advName = advertise.getAdvName();
		startDate = FastDateFormat.getInstance(EcConstant.PATTERN_DATE).format(advertise.getStartDate());
		endDate = FastDateFormat.getInstance(EcConstant.PATTERN_DATE).format(advertise.getEndDate());
		startTime = FastDateFormat.getInstance("HH:mm").format(advertise.getStartDate());
		endTime = FastDateFormat.getInstance("HH:mm").format(advertise.getEndDate());
		createDate = advertise.getCreateDate();
		createUser = advertise.getCreateUser();
		modifyDate = advertise.getModifyDate();
		modifyUser = advertise.getModifyUser();
		
		readPropertise(content);
		
		advId = null;
		
		return "editTop4Tag";
	}
	
	/**
	 * @return
	 * @throws Exception
	 */
	public String saveTop4Tag() throws Exception {
		boolean result = false;
		if (top4TagValidate()) {
			if (StringUtils.isBlank(advId)) {// add
				advertise = new GenerateAdvertise();
				position = new GeneratePosition();
				content = new GenerateContent();
			} else {// update
				advertise = generateService.get(Long.parseLong(advId));
				position = generateService.getGeneratePositionByAdvId(Long.parseLong(advId)).get(0);
				content = generateService.getGenerateContentByAdvId(Long.parseLong(advId)).get(0);
			}
			
			advertise.setAdvType(advType);
			advertise.setAdvName(advName.trim());
			advertise.setStartDate(PATTERN_DATE_FORMAT.parse(startDate + " " + startTime + ":00"));
			advertise.setEndDate(PATTERN_DATE_FORMAT.parse(endDate + " " + endTime + ":59"));
			advertise.setAdvStatus(advStatus);
			
			contents = new StringBuilder("");
			contents.append("title1Name=").append(title1Name.trim()).append("\n");
			contents.append("isHighlight1=").append(isHighlight1).append("\n");
			contents.append("title1Url=").append(checkURL(title1Url)).append("\n");
			if (!StringUtils.isBlank(title1adWord)) {
				contents.append("title1adWord=").append(title1adWord.trim()).append("\n");
			}
			
			contents.append("title2Name=").append(title2Name.trim()).append("\n");
			contents.append("isHighlight2=").append(isHighlight2).append("\n");
			contents.append("title2Url=").append(checkURL(title2Url)).append("\n");
			if (!StringUtils.isBlank(title2adWord)) {
				contents.append("title2adWord=").append(title2adWord.trim()).append("\n");
			}
			
			contents.append("title3Name=").append(title3Name.trim()).append("\n");
			contents.append("isHighlight3=").append(isHighlight3).append("\n");
			contents.append("title3Url=").append(checkURL(title3Url)).append("\n");
			if (!StringUtils.isBlank(title3adWord)) {
				contents.append("title3adWord=").append(title3adWord.trim()).append("\n");
			}
			
			contents.append("title4Name=").append(title4Name.trim()).append("\n");
			contents.append("isHighlight4=").append(isHighlight4).append("\n");
			contents.append("title4Url=").append(checkURL(title4Url)).append("\n");
			if (!StringUtils.isBlank(title4adWord)) {
				contents.append("title4adWord=").append(title4adWord.trim()).append("\n");
			}
			
			content.setContents(contents.toString());
			
			if (StringUtils.isBlank(advId)) {
				result = generateService.saveGenerateCombine(advertise, position, content);
			} else {
				result = generateService.updateGenerateCombine(advertise, position, content);
			}
			
			contents.setLength(0);
			
			this.generateService.generateTop4Tag();
			
			if (!result) {
				addActionMessage("储存失败");
			} else {
				addActionMessage("储存成功");
			}
		}
		
		return "editTop4Tag";
	}
	
	private boolean top4TagValidate() throws CoreException, ParseException, IOException {
		/**************************** 必填栏位 ****************************/
		logger.info("advName = " + advName);

		if (StringUtils.isBlank(advName)) {
			addFieldError("advName", "请输入广告名称");
		}

		if (StringUtils.isBlank(startDate)) {
			addFieldError("startDate", "请输入起始日期");
		}
		if (StringUtils.isBlank(startTime)) {
			addFieldError("startTime", "请输入起始时间");
		}
		if (StringUtils.isBlank(endDate)) {
			addFieldError("endDate", "请输入结束日期");
		}
		if (StringUtils.isBlank(endTime)) {
			addFieldError("endTime", "请输入结束时间");
		}
		
		if (StringUtils.isBlank(title1Name)) {
			addFieldError("title1Name", "请输入标题1的文字标题");
		}
		if (StringUtils.isBlank(title1Url)) {
			addFieldError("title1Url", "请输入标题1的点击连结");
		}
		
		if (StringUtils.isBlank(title2Name)) {
			addFieldError("title2Name", "请输入标题2的文字标题");
		}
		if (StringUtils.isBlank(title2Url)) {
			addFieldError("title2Url", "请输入标题2的点击连结");
		}
		
		if (StringUtils.isBlank(title3Name)) {
			addFieldError("title3Name", "请输入标题3的文字标题");
		}
		if (StringUtils.isBlank(title3Url)) {
			addFieldError("title3Url", "请输入标题3的点击连结");
		}
		
		if (StringUtils.isBlank(title4Name)) {
			addFieldError("title4Name", "请输入标题4的文字标题");
		}
		if (StringUtils.isBlank(title4Url)) {
			addFieldError("title4Url", "请输入标题4的点击连结");
		}

		if (getFieldErrors().isEmpty()) {
			return true;
		} else {
			return false;
		}
	}
	
	private String checkURL(String url) {
		if (!(url.substring(0, 5).equals("https") || url.substring(0, 4).equals("http")
				|| url.substring(0, 3).equals("ftp") || url.substring(0, 4).equals("rtsp") || url.substring(0, 3).equals("mms"))) {
			return "http://" + url;
		}
		return url;
	}
	
	/**
	 * 初始化变数值
	 */
	private void initVariables() {
		advertise = new GenerateAdvertise();
		position = new GeneratePosition();
		content = new GenerateContent();

		advType = "";
		advStatus = "";
		advName = "";
		startDate = "";
		endDate = "";
		startTime = "";
		endTime = "";
		createDate = null;
		createUser = "";
		modifyDate = null;
		modifyUser = "";
		
		title1Name = "";
		isHighlight1 = "";
		title1Url = "";
		title1adWord = "";
		
		title2Name = "";
		isHighlight2 = "";
		title2Url = "";
		title2adWord = "";
		
		title3Name = "";
		isHighlight3 = "";
		title3Url = "";
		title3adWord = "";
		
		title4Name = "";
		isHighlight4 = "";
		title4Url = "";
		title4adWord = "";
	}
	
	/**
	 * 從db讀取propertise資料
	 * 
	 * @param content
	 * @throws IOException
	 */
	private void readPropertise(GenerateContent content) throws IOException {
		Properties prop = new Properties();
		Reader reader = new StringReader(content.getContents());
		prop.load(reader);
		reader.close();
		
		title1Name = StringUtils.defaultString(prop.get("title1Name").toString());
		isHighlight1 = StringUtils.defaultString(prop.get("isHighlight1").toString());
		title1Url = StringUtils.defaultString(prop.get("title1Url").toString());
		if (prop.containsKey("title1adWord")) {
			title1adWord = StringUtils.defaultString(prop.get("title1adWord").toString(), "");
		}
		
		title2Name = StringUtils.defaultString(prop.get("title2Name").toString());
		isHighlight2 = StringUtils.defaultString(prop.get("isHighlight2").toString());
		title2Url = StringUtils.defaultString(prop.get("title2Url").toString());
		if (prop.containsKey("title2adWord")) {
			title2adWord = StringUtils.defaultString(prop.get("title2adWord").toString(), "");
		}
		
		title3Name = StringUtils.defaultString(prop.get("title3Name").toString());
		isHighlight3 = StringUtils.defaultString(prop.get("isHighlight3").toString());
		title3Url = StringUtils.defaultString(prop.get("title3Url").toString());
		if (prop.containsKey("title3adWord")) {
			title3adWord = StringUtils.defaultString(prop.get("title3adWord").toString(), "");
		}
		
		title4Name = StringUtils.defaultString(prop.get("title4Name").toString());
		isHighlight4 = StringUtils.defaultString(prop.get("isHighlight4").toString());
		title4Url = StringUtils.defaultString(prop.get("title4Url").toString());
		if (prop.containsKey("title4adWord")) {
			title4adWord = StringUtils.defaultString(prop.get("title4adWord").toString(), "");
		}
	}

	//getter & setter
	public GenerateService getGenerateService() {
		return generateService;
	}

	public void setGenerateService(GenerateService generateService) {
		this.generateService = generateService;
	}
	
	public String getAdvType() {
		return advType;
	}

	public void setAdvType(String advType) {
		this.advType = advType;
	}

	public String getAdvStatus() {
		return advStatus;
	}

	public void setAdvStatus(String advStatus) {
		this.advStatus = advStatus;
	}

	public String getAdvId() {
		return advId;
	}

	public void setAdvId(String advId) {
		this.advId = advId;
	}

	public String getAdvName() {
		return advName;
	}

	public void setAdvName(String advName) {
		this.advName = advName;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	public Date getModifyDate() {
		return modifyDate;
	}

	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}

	public String getModifyUser() {
		return modifyUser;
	}

	public void setModifyUser(String modifyUser) {
		this.modifyUser = modifyUser;
	}

	public String getTitle1Name() {
		return title1Name;
	}

	public void setTitle1Name(String title1Name) {
		this.title1Name = title1Name;
	}

	public String getIsHighlight1() {
		return isHighlight1;
	}

	public void setIsHighlight1(String isHighlight1) {
		this.isHighlight1 = isHighlight1;
	}

	public String getTitle1Url() {
		return title1Url;
	}

	public void setTitle1Url(String title1Url) {
		this.title1Url = title1Url;
	}

	public String getTitle1adWord() {
		return title1adWord;
	}

	public void setTitle1adWord(String title1adWord) {
		this.title1adWord = title1adWord;
	}

	public String getTitle2Name() {
		return title2Name;
	}

	public void setTitle2Name(String title2Name) {
		this.title2Name = title2Name;
	}

	public String getIsHighlight2() {
		return isHighlight2;
	}

	public void setIsHighlight2(String isHighlight2) {
		this.isHighlight2 = isHighlight2;
	}

	public String getTitle2Url() {
		return title2Url;
	}

	public void setTitle2Url(String title2Url) {
		this.title2Url = title2Url;
	}

	public String getTitle2adWord() {
		return title2adWord;
	}

	public void setTitle2adWord(String title2adWord) {
		this.title2adWord = title2adWord;
	}

	public String getTitle3Name() {
		return title3Name;
	}

	public void setTitle3Name(String title3Name) {
		this.title3Name = title3Name;
	}

	public String getIsHighlight3() {
		return isHighlight3;
	}

	public void setIsHighlight3(String isHighlight3) {
		this.isHighlight3 = isHighlight3;
	}

	public String getTitle3Url() {
		return title3Url;
	}

	public void setTitle3Url(String title3Url) {
		this.title3Url = title3Url;
	}

	public String getTitle3adWord() {
		return title3adWord;
	}

	public void setTitle3adWord(String title3adWord) {
		this.title3adWord = title3adWord;
	}

	public String getTitle4Name() {
		return title4Name;
	}

	public void setTitle4Name(String title4Name) {
		this.title4Name = title4Name;
	}

	public String getIsHighlight4() {
		return isHighlight4;
	}

	public void setIsHighlight4(String isHighlight4) {
		this.isHighlight4 = isHighlight4;
	}

	public String getTitle4Url() {
		return title4Url;
	}

	public void setTitle4Url(String title4Url) {
		this.title4Url = title4Url;
	}

	public String getTitle4adWord() {
		return title4adWord;
	}

	public void setTitle4adWord(String title4adWord) {
		this.title4adWord = title4adWord;
	}
}